package com.example.demo.controller;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author krystal
 * @date 2023/3/27 12:31
 */
@RestController
public class RedisAnnController {
    //存入或读取内存
    @RequestMapping("/ann-get")
    @Cacheable(value = "spring.cache",key = "#name+'-'+#pnumber")
    public String get(String name,String pnumber){
        if (!StringUtils.hasLength(name) || !StringUtils.hasLength(pnumber)){
            return null;
        }
        System.out.println("执行了get方法");
        return "name="+name+"|pnumber"+pnumber;
    }

    //更新缓存
    @RequestMapping("/put")
    @CachePut(value = "spring.cache",key = "#name+'-'+#pnumber")
    public String put(String name,String pnumber){
        if (!StringUtils.hasLength(name) || !StringUtils.hasLength(pnumber)){
            return null;
        }
        System.out.println("执行了put方法");
        return "[name="+name+"<->pnumber"+pnumber+"]";
    }

    //redis缓存删除
    @RequestMapping("/del")
    @CacheEvict(value = "spring.cache",key = "#name+'-'+#pnumber")
    public void del(String name,String pnumber){
        System.out.println("执行了缓存删除");
    }
}
